package com.sso.config;




import com.sso.realm.HomeMyRealm;
import com.sso.realm.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import javax.servlet.Filter;
import java.util.Arrays;
import java.util.HashMap;


/**
 * @create 2022-08-07
 */
@Configuration
public class ShiroConfig {

    @Bean
    public DefaultWebSecurityManager securityManager (){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealms(Arrays.asList(realm(),homeRealm()));
        return defaultWebSecurityManager;
    }
    @Bean
    public Realm realm(){
        MyRealm myRealm  = new MyRealm();
        myRealm.setCredentialsMatcher(credentialsMatcher());
        return myRealm;
    }
    @Bean
    public Realm homeRealm(){
        HomeMyRealm homeMyRealm = new HomeMyRealm();
        homeMyRealm.setCredentialsMatcher(credentialsMatcher());
        return homeMyRealm;
    }
    @Bean
    public CredentialsMatcher credentialsMatcher (){
        HashedCredentialsMatcher credentialsMatcher  = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("MD5");
        credentialsMatcher.setHashIterations(1024);
        return credentialsMatcher;
    }
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean filterFactoryBean(){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        factoryBean.setSecurityManager(securityManager());
        //设置拦截规则
        HashMap<String,String> map = new HashMap<>();
        map.put("/sso/login","anon");
        map.put("/sso/homeLogin","anon");
        map.put("/doc.html","anon");
        map.put("/*.js","anon");
        map.put("/swagger-ui.html", "anon");
        map.put("/swagger-resources/**", "anon");
        map.put("/v2/**", "anon");
        map.put("/webjars/**", "anon");
        map.put("/","anon");
        map.put("/csrf","anon");
        map.put("/**","authc");
        map.put("/sso/homeTuichuLogin","anon");

        factoryBean.setFilterChainDefinitionMap(map);
        //设置自定义认证过滤器
/*        HashMap<String, Filter> filterMap = new HashMap<>();
        filterMap.put("authc",new LoginFilter());
        factoryBean.setFilters(filterMap);*/
        return factoryBean;
    }

    @Bean//注册filter 三大组件
    public FilterRegistrationBean<Filter> filterRegistrationBean(){
        FilterRegistrationBean<Filter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setName("shiroFilter");
        filterRegistrationBean.setFilter(new DelegatingFilterProxy());

        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }
    //开始shiro注解
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager());
        return authorizationAttributeSourceAdvisor;
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }


}
